import router from "@/router";
const ENV = require(`./environ/${process.env.NODE_ENV}`).default //  引入环境文件
const AppModule = {
  state: {
    //  全局 ajax 计数
    ajax_request_count: 0,
    env: ENV,
    scroll_forbiden: false,
    //  添加进来的 tabs
    tabs: [],
    slash_is_active: false,  //  激活框是否被激活
    sidebar_width: 240  //  侧边栏的宽度
  },
  getters: {
    tabs(state) {
      //  归并默认 tabs 以及添加路由
      const defaulttabs = router.options.routes.filter(route => {
        return !route.hideinmenu
      })
      return [...defaulttabs, ...state.tabs];
    },
    slash_is_active(state) {
      return state.slash_is_active
    },
    sidebar_width(state) {
      return state.sidebar_width
    }
  },
  mutations: {
    SET_AJAX_REQUEST_COUNT(state, payload) {
      state.ajax_request_count = state.ajax_request_count + payload
    },
    SET_SCROLL_FORBIDEN(state, payload) {
      state.scroll_forbiden = payload;
    },
    //  添加 tabs 仅在 tabs.vue 里面监听 路由变化的时候使用
    PUSH_TABS(state, payload) {
      const exists = [...router.options.routes, ...state.tabs].some((tab) => tab.path === payload.path);
      if (exists) {
        //  已经存在该 tab
        return
      }
      state.tabs.push(payload);
    },
    //  删除 tabs
    DELETE_TABS(state, payload) {
      let index = 0;
      for (let i = 0; i < state.tabs.length; i++) {
        if (state.tabs[i].path === payload.path) {
          index = i;
          break;
        }
      }
      state.tabs.splice(index, 1);
    },
    SLASH_ACTIVE(state, payload) {
      state.slash_is_active = payload
    },
    RESET_SIDEBAR_WIDTH(state, payload) {
      state.sidebar_width = payload
    }
  },
  actions: {
    SET_SCROLL_FORBIDEN(context, payload) {
      context.commit("SET_SCROLL_FORBIDEN", payload);
    }
  }
}
export default AppModule;
